﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StuManagementSystem
{
    public partial class frm_AddStuMark : Form
    {
        DbContext db = new DbContext();
        private int StuCode ;
        private bool IsChange = false;
        private int ClassStuMarkId;
        public frm_AddStuMark()
        {
            InitializeComponent();
        }

        public frm_AddStuMark(int StuCode)
        {
            InitializeComponent();
            //不允许自动创建列
            this.dgv_StuMark.AutoGenerateColumns = false;
            this.StuCode = StuCode;
            //DataGridView整行选中
            this.dgv_StuMark.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //DataGridView不允许出现空行
            this.dgv_StuMark.AllowUserToAddRows = false;
            //不允许多选
            this.dgv_StuMark.MultiSelect = false;
            //加载科目数据
            var sql = string.Format(@"
                    SELECT -1 StuSubjectId,'-请选择-' StuSubjectName
                    union
                    SELECT StuSubjectId,StuSubjectName FROM dbo.StuSubject,StuGrade,dbo.StuInfo where StuGrade.StuGradeId = StuSubject.StuGradeId AND StuInfo.StuGradeId = StuGrade.StuGradeId AND dbo.StuInfo.StuCode ={0}", StuCode);
            DataSet ds = db.ExecDataSet(sql);
            var dt = ds.Tables[0];
            this.cbo_StuSubject.DisplayMember = "StuSubjectName";
            this.cbo_StuSubject.ValueMember = "StuSubjectId";
            this.cbo_StuSubject.DataSource = dt;
        }

        #region 窗口预加载
        /// <summary>
        /// 窗口预加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_AddStuMark_Load(object sender, EventArgs e)
        {
            SelectStuMark();
        }
        #endregion

        #region 添加 或 修改按钮
        /// <summary>
        /// 添加 或 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AddStuMark_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (!CheckIsNull())
                return;
            var StuSubject = Convert.ToInt32(this.cbo_StuSubject.SelectedValue);
            var StuMark = this.txt_StuMark.Text;
            var StuExamTime = this.ckl_StuExamTime.Value;
            if (IsChange)
            {
                sql = string.Format(@"UPDATE dbo.StuMark SET StuMark = {0},StuExamTime = '{1}' WHERE StuMarkId = {2}",StuMark,StuExamTime, ClassStuMarkId);
                this.IsChange = false;
            }
            else
            {
                sql = string.Format(@"SELECT COUNT(1) FROM dbo.StuMark WHERE StuSubjectId = {0} AND StuCode = {1}", StuSubject, this.StuCode);
                if (Convert.ToInt32(db.ExecScaler(sql)) > 0)
                {
                    MessageBox.Show("不要重复插入");
                    return ;
                }
                sql = string.Format(@"INSERT INTO dbo.StuMark
(
    StuCode,
    StuSubjectId,
    StuMark,
    StuExamTime
)
VALUES
(   {0},        -- StuCode - int
    {1},        -- StuSubjectId - int
    {2},        -- StuMark - int
    '{3}' -- StuExamTime - datetime
    )", this.StuCode, StuSubject, StuMark, StuExamTime);
            }
            if (db.ExecNonQuery(sql))
            {
                MessageBox.Show(this.btn_AddStuMark.Text + "成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.btn_AddStuMark.Text = "添加";
                this.cbo_StuSubject.Enabled = true;
            }
            else
                MessageBox.Show(this.btn_AddStuMark.Text + "失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            SelectStuMark();
        }
        #endregion

        #region 私有检查方法
        private bool CheckIsNull()
        {
            if (string.IsNullOrWhiteSpace(this.cbo_StuSubject.Text) || this.cbo_StuSubject.Text.Equals("-请选择-"))
            {
                MessageBox.Show("请选择科目");
                this.cbo_StuSubject.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuMark.Text))
            {
                MessageBox.Show("请填写成绩");
                this.txt_StuMark.Focus(); 
                return false;
            }
            return true;
        }
        #endregion

        #region 私有查询方法
        /// <summary>
        /// 查询现有的学生成绩
        /// </summary>
        private void SelectStuMark()
        {
            string sql = string.Format(@"SELECT StuMarkId,StuName ,StuSubjectName,dbo.StuMark.StuMark,StuExamTime FROM dbo.StuInfo,dbo.StuMark,dbo.StuSubject WHERE StuInfo.StuCode = StuMark.StuCode AND StuMark.StuSubjectId = StuSubject.StuSubjectId AND StuInfo.StuGradeId = StuSubject.StuGradeId AND  StuInfo.StuCode = '{0}'", this.StuCode);
            var ds = db.ExecDataSet(sql);
            this.dgv_StuMark.DataSource = ds.Tables[0];
        }
        #endregion
        
        #region 返回
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Back_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 右键

        /// <summary>
        /// 修改成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cms_ChangeStuMark_Click(object sender, EventArgs e)
        {
            int StuMarkId = Convert.ToInt32(this.dgv_StuMark.CurrentRow.Cells["StuMarkId"].Value);
            this.ClassStuMarkId = StuMarkId;
            string sql = string.Format(@"SELECT * FROM dbo.StuMark WHERE StuMarkId = {0}", StuMarkId);
            DataSet ds = db.ExecDataSet(sql);
            DataTable dt = ds.Tables[0];
            cbo_StuSubject.SelectedValue = dt.Rows[0]["StuSubjectId"];
            txt_StuMark.Text = dt.Rows[0]["StuMark"].ToString();
            ckl_StuExamTime.Value = Convert.ToDateTime(dt.Rows[0]["StuExamTime"]);
            this.IsChange = true;
            this.btn_AddStuMark.Text = "修改";
            this.txt_StuMark.Focus();
            this.cbo_StuSubject.Enabled = false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cms_Delete_Click(object sender, EventArgs e)
        {
            int StuMarkId = Convert.ToInt32(this.dgv_StuMark.CurrentRow.Cells["StuMarkId"].Value);
            string sql = string.Format(@"DELETE FROM dbo.StuMark WHERE StuMarkId = {0}", StuMarkId);
            if (db.ExecNonQuery(sql))
                MessageBox.Show("删除成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("删除失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            SelectStuMark();
        }
        #endregion 

 



        
    }
}
